<?php

namespace App\Entity\MyData;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Base\Annotation\Common;
use App\Repository\MyData\MyDataKeyRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass=MyDataKeyRepository::class)
 */
class MyDataKey
{
    /**
     * @Common(cnName="#",listType="text",editType="text")
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"mydata"})
     */
    private $id;

    /**
     * @Common(cnName="中文字段",listType="text",editType="text",width="200")
     * @ORM\Column(type="string", length=255)
     * @Groups({"mydata"})
     */
    private $label;

    /**
     * @Common(cnName="英文字段",listType="text",editType="text",width="200")
     * @ORM\Column(type="string", length=255)
     * @Groups({"mydata"})
     */
    private $name;

    /**
     * @Common(cnName="字段类型",listType="text",editType="select",width="200")
     * @ORM\Column(type="string", length=255)
     * @Groups({"mydata"})
     */
    private $type;

    /**
     * @Common(cnName="所属模板",listType="text",editType="entity",width="200")
     * @ORM\ManyToOne(targetEntity=MyDataTemplate::class, inversedBy="myKeys")
     * @ORM\JoinColumn(nullable=false)
     * @Groups({"mydata"})
     */
    private $template;

    /**
     * @Common(cnName="字段集",listType="text",editType="entity")
     * @ORM\OneToMany(targetEntity=MyDataValue::class, mappedBy="myKey")
     * @Groups({"values"})
     */
    private $myValues;

    /**
     * @Common(cnName="附加值",listType="text",editType="textarea",width="")
     * @ORM\Column(type="text", nullable=true)
     * @Groups({"mydata"})
     */
    private $param;

    public function __construct()
    {
        $this->myValues = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getLabel(): ?string
    {
        return $this->label;
    }

    public function setLabel(string $label): self
    {
        $this->label = $label;

        return $this;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getType(): ?string
    {
        return $this->type;
    }

    public function setType(string $type): self
    {
        $this->type = $type;

        return $this;
    }

    public function getTemplate(): ?MyDataTemplate
    {
        return $this->template;
    }

    public function setTemplate(?MyDataTemplate $template): self
    {
        $this->template = $template;

        return $this;
    }

    /**
     * @return Collection|MyDataValue[]
     */
    public function getMyValues(): Collection
    {
        return $this->myValues;
    }

    public function addMyValue(MyDataValue $myValue): self
    {
        if (!$this->myValues->contains($myValue)) {
            $this->myValues[] = $myValue;
            $myValue->setMyKey($this);
        }

        return $this;
    }

    public function removeMyValue(MyDataValue $myValue): self
    {
        if ($this->myValues->contains($myValue)) {
            $this->myValues->removeElement($myValue);
            // set the owning side to null (unless already changed)
            if ($myValue->getMyKey() === $this) {
                $myValue->setMyKey(null);
            }
        }

        return $this;
    }

    public function getParam(): ?string
    {
        return $this->param;
    }

    public function setParam(?string $param): self
    {
        $this->param = $param;

        return $this;
    }
}
